package com.yunji.ylexpiration.Controller;

import com.yunji.ylexpiration.entity.FurnCommodity;
import com.yunji.ylexpiration.entity.FurnOrders;
import com.yunji.ylexpiration.entity.FurnShoppinTrolley;
import com.yunji.ylexpiration.service.FurnCommodityService;
import com.yunji.ylexpiration.service.FurnOrdersMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.resource.HttpResource;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
public class FurnOrdersController {
    @Autowired
    FurnOrdersMapperService furnOrdersMapperService;
    @Autowired
    FurnCommodityService furnCommodityService;
    @RequestMapping("seleorders")
    public String seleorders(Model model,Integer ordersso,HttpSession session){
        if(ordersso==null){
            session.setAttribute("ordersso",0);
        }else{
            session.setAttribute("ordersso",ordersso);
        }
//       List<FurnOrders> furnOrders = furnOrdersMapperService.selFurnOrders (1);
        List<FurnOrders> furnOrders10 = furnOrdersMapperService.selFurnOrdersbyStatus ("10", 1);
        List<FurnOrders> furnOrders20 = furnOrdersMapperService.selFurnOrdersbyStatus ("20", 1);
        List<FurnOrders> furnOrders30 = furnOrdersMapperService.selFurnOrdersbyStatus ("30", 1);
//       for (int i = 0; i <furnOrders.size () ; i++) {
//                   String[] ss2 = furnOrders.get (i).getCommodityId ().split(",");//使用字符串逗号 ,切割字符串
//                   String[] ss3 = furnOrders.get (i).getClassificationtypeId ().split(",");//使用字符串逗号 ,切割字符串
//                   List<FurnCommodity> furnCommodities= new ArrayList<> ();
//                   for(int j = 0; j <ss2.length ; j++){
//                       FurnCommodity furnCommodity = furnCommodityService.selectByid (Integer.valueOf (ss2[i]));
//                       furnCommodity.setCorreId (Integer.valueOf (ss3[j]));
//                       furnCommodities.add (furnCommodity);
//                   }
//                   furnOrders.get (i).setComm (furnCommodities);
//           }
        for (int i = 0; i <furnOrders10.size () ; i++) {
            String[] ss2 = furnOrders10.get (i).getCommodityId ().split(",");//使用字符串逗号 ,切割字符串
            String[] ss3 = furnOrders10.get (i).getClassificationtypeId ().split(",");//使用字符串逗号 ,切割字符串
            List<FurnCommodity> furnCommodities= new ArrayList<> ();
            for(int j = 0; j <ss2.length ; j++){
                FurnCommodity furnCommodity = furnCommodityService.selectByid (Integer.valueOf (ss2[j]));
                furnCommodity.setCorreId (Integer.valueOf (ss3[j]));
                furnCommodities.add (furnCommodity);
            }
            furnOrders10.get (i).setComm (furnCommodities);
        }
        for (int i = 0; i <furnOrders20.size () ; i++) {
            String[] ss2 = furnOrders20.get (i).getCommodityId ().split(",");//使用字符串逗号 ,切割字符串
            String[] ss3 = furnOrders20.get (i).getClassificationtypeId ().split(",");//使用字符串逗号 ,切割字符串
            List<FurnCommodity> furnCommodities= new ArrayList<> ();
            for(int j = 0; j <ss2.length ; j++){
                FurnCommodity furnCommodity = furnCommodityService.selectByid (Integer.valueOf (ss2[j]));
                furnCommodity.setCorreId (Integer.valueOf (ss3[j]));
                furnCommodities.add (furnCommodity);
            }
            furnOrders20.get (i).setComm (furnCommodities);
        }
        for (int i = 0; i <furnOrders30.size () ; i++) {
            String[] ss2 = furnOrders30.get (i).getCommodityId ().split(",");//使用字符串逗号 ,切割字符串
            String[] ss3 = furnOrders30.get (i).getClassificationtypeId ().split(",");//使用字符串逗号 ,切割字符串
            List<FurnCommodity> furnCommodities= new ArrayList<> ();
            for(int j = 0; j <ss2.length ; j++){
                FurnCommodity furnCommodity = furnCommodityService.selectByid (Integer.valueOf (ss2[j]));
                furnCommodity.setCorreId (Integer.valueOf (ss3[j]));
                furnCommodities.add (furnCommodity);
            }
            furnOrders30.get (i).setComm (furnCommodities);
        }
        model.addAttribute ("furnOrders10",furnOrders10);
        model.addAttribute ("furnOrders20",furnOrders20);
        model.addAttribute ("furnOrders30",furnOrders30);
        return "myorderq";
    }
    @GetMapping("mygxin.html")
    public String  countByorder(HttpSession session) {
        Object userid = session.getAttribute("userid");
        if(userid!=null) {
            List<FurnOrders> furnOrders = furnOrdersMapperService.countByorder("10",Integer.valueOf((int)userid));
            List<FurnOrders> furnOrders2 = furnOrdersMapperService.countByorder("20",Integer.valueOf((int)userid));
            List<FurnOrders> furnOrders3 = furnOrdersMapperService.countByorder("30",Integer.valueOf((int)userid));
            if(furnOrders==null||furnOrders.size()==0){
                session.setAttribute("furnOrders10", 0);
            }else{
                session.setAttribute("furnOrders10", furnOrders.size());
            }
            if(furnOrders2==null||furnOrders2.size()==0){
                session.setAttribute("furnOrders20", 0);
            }else{
                session.setAttribute("furnOrders20", furnOrders2.size());
            }
            if(furnOrders3==null||furnOrders3.size()==0){
                session.setAttribute("furnOrders30", 0);
            }else{
                session.setAttribute("furnOrders30", furnOrders3.size());
            }
        }else {
            session.setAttribute("furnOrders10", 0);
            session.setAttribute("furnOrders20", 0);
            session.setAttribute("furnOrders30", 0);
        }
        return "mygxin";
    }
    @RequestMapping("shouh.html")
    public String uptshouh(String orderId){
        FurnOrders furnOrders =new FurnOrders ();
        furnOrders.setOrderNum (orderId);
        furnOrders.setOrderStatus (String.valueOf (30));
        int i = furnOrdersMapperService.upFurnOrders (furnOrders);
        return "redirect:seleorders";
    }
}